package com.dyzs.client;

import java.io.ByteArrayInputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

import org.xmlpull.v1.XmlPullParser;

import android.util.Xml;

import com.dyzs.util.Constants;
import com.dyzs.util.WSUtils;

public class WeatherClient {


	public String update(String cityName) throws Exception {
		Map<String, Object> params = new TreeMap<String, Object>();
		params.put("arg0", cityName);					
		try {
			String resultXml = WSUtils.callWS("getWeather",params, Constants.ToolURL);			
			return resultXml;
		} catch (Exception e) {
			throw  e;
		}
	}


	
	public Map<String,Object> pullXml(String resultXml) throws Exception {	
		if(resultXml.equals("W")){
			throw new Exception("服务端异常！");
		}else if(resultXml.equals("N")){
			throw new Exception("暂无数据！");
		}else{
			 Map<String,Object> map=null;		
				try {
					ByteArrayInputStream bin = new ByteArrayInputStream(resultXml.getBytes());
					InputStreamReader in = new InputStreamReader(bin);
					XmlPullParser parser = Xml.newPullParser();
					parser.setInput(in);
					int event = parser.getEventType();
					while (event != XmlPullParser.END_DOCUMENT) {				
						switch (event) {
						case XmlPullParser.START_DOCUMENT:
							map=new HashMap<String,Object>();
							break;
						case XmlPullParser.START_TAG:					
							if ("zhuantai".equals(parser.getName())) {
								map.put("zhuantai", parser.nextText());
							} else if ("qiwen".equals(parser.getName())) {
								map.put("qiwen", parser.nextText());
							} else if ("fenxian".equals(parser.getName())) {
								map.put("fenxian", parser.nextText());
							} else if ("shidu".equals(parser.getName())) {
								map.put("shidu", parser.nextText());
							} else if ("ziwaixian".equals(parser.getName())) {
								map.put("ziwaixian", parser.nextText());
							} else if ("wendu1".equals(parser.getName())) {
								map.put("wendu1", parser.nextText());
							}else if ("zhuantai2".equals(parser.getName())) {
								map.put("zhuantai2", parser.nextText());
							}else if ("wendu2".equals(parser.getName())) {
								map.put("wendu2", parser.nextText());
							}else if ("zhuantai3".equals(parser.getName())) {
								map.put("zhuantai3", parser.nextText());
							}else if ("wendu3".equals(parser.getName())) {
								map.put("wendu3", parser.nextText());
							}
							break;				
						}
						event = parser.next();
					}
				} catch (Exception e) {
					e.printStackTrace();
					throw new Exception("解析失败！", e);
				}
				return map;
		}
		
		
	   
	}

}
